const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const cors = require('cors')
app.use(bodyParser.urlencoded({extend:true}))
app.use(bodyParser.json())
app.use(cors())
const dbport = 3306
const appport = 8080
// -----------------
const mysql = require('mysql')
const DBNAME = "adminprogramedb"
const TABLENAME = "user_table"
// -----------------
const getusertable = require('./user_table')
const connection = mysql.createConnection({
    host: "localhost",
    port:dbport,
    user: "root",
    password: "123456",
    database: DBNAME
})
let usertablelist
const getusertablecallback = (callback) => {
    // 回调
    usertablelist = callback
    console.log(usertablelist, 'tablelist');
}
getusertable(connection, getusertablecallback)

app.post('/logByAccount', (request, response) => {
    console.log('request: ', request.body);
    const item = usertablelist.find(item =>
        item.account === request.body.params.account && item.password === request.body.params.password
    )
    console.log(item,'item');
    let res = {}
    if(item){
        res = {
            message: '请求成功',
            code: 200,
            method: 'POST',
            wayForInfo: "request.body",
            data: item
        }
    }else{
        res = {
            message: '账户或者密码不正确',
            code: 200,
            method: 'POST',
            wayForInfo: "request.body",
            data: null
        }
    }
    response.send(res)
})
app.listen(appport,()=>{
    console.log('端口：'+appport+'启动成功！');
})
